package sample.model;

import com.jcraft.jsch.Session;

import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.Callable;

import static sample.util.SSHUtils.execCommandByJSch;

public class StopMonitor implements Callable<String> {
    private HashMap<String, String[]> serverMap = new HashMap<String, String[]>();
    private HashMap<String, Session> serverSessionMap = new HashMap<String, Session>();
    private static Boolean flag ;
    private Iterator it ;
    private int mapsize ;
    private String[] keys;
    int keynum ;
    String str;
    //执行参数
    String fuwuqixinxi = "";
    String nmonbushulujing = "";
    String pinglv = "";
    String cishu = "";
    String name = "";

    public StopMonitor(HashMap serverMap,HashMap serverSessionMap, int keynum,String nmonbushulujing ,String pinglv,String cishu,String name){
        this.serverMap = serverMap;
        this.serverSessionMap = serverSessionMap;
        this.keynum = keynum;
        this.flag = true;
        this.mapsize = serverMap.size();
        this.it = serverMap.keySet().iterator();
        this.keys = new String[mapsize];
        this.nmonbushulujing = nmonbushulujing;
        this.pinglv = pinglv;
        this.cishu = cishu;
        this.name = name;
        for (int i = 0; i < mapsize; i++) {
            keys[i] = it.next().toString();
        }
    }

    @Override
    public String call() throws Exception {
        String str = "";
        while (flag){
            str = startNmon();
        }
        if (str.length()>0){
            return "执行错误："+str;
        }
        return str;
    }

    private String startNmon() {
        String result = "";
        String str = getMapskey();
        try {
            //如果为linux
            if(execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8").toUpperCase().contains("LINUX")) {
                 result = execCommandByJSch(serverSessionMap.get(str), "ps -ef|grep "+name+"|grep -v grep|awk '{print $2}'", "UTF-8");
                if (result.length() != 0) {
                    result=execCommandByJSch(serverSessionMap.get(str), "kill -9 " + result, "UTF-8");
                }else {
                    result="监控任务不存在";
                }
            }else if(execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8").toUpperCase().contains("AIX")) {
                result = execCommandByJSch(serverSessionMap.get(str), "ps -ef|grep "+name+"|grep -v grep|awk '{print $2}'", "UTF-8");
                if (result.length() != 0) {
                    result=execCommandByJSch(serverSessionMap.get(str), "kill -9 " + result, "UTF-8");
                }  else {
                    result="监控任务不存在";
                }
            }
        } catch (Exception e) {
            System.out.println(serverSessionMap.get(str).getHost()+"停止监控 error："+e.getMessage());
            return "[error]"+e.getMessage();
        }
        return result;
    }



    public synchronized String getMapskey(){
        String key = keys[keynum];
        keynum++;
        if(keynum >= mapsize){
            flag = false;

        }
        // System.out.println("线程"+Thread.currentThread().getName()+"获取了key"+key+",此时的 keynum已经是:"+keynum );
        return key;
    }
}